from sqlalchemy import (
    Column,
    Integer,
    String,
    Float,
    DateTime,
    Boolean,
    Text,
    ForeignKey,
    Enum,
)
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.core.database import Base
import enum


# 枚举定义
class UserRole(str, enum.Enum):
    admin = "admin"
    operator = "operator"


class RoomStatus(str, enum.Enum):
    active = "active"
    maintenance = "maintenance"
    closed = "closed"


class MachineStatus(str, enum.Enum):
    available = "available"
    in_use = "in_use"
    maintenance = "maintenance"


class SessionStatus(str, enum.Enum):
    active = "active"
    completed = "completed"
    cancelled = "cancelled"


# 数据模型
class User(Base):
    __tablename__ = "users"

    id_ = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    password_hash = Column(String(255), nullable=False)
    role = Column(Enum(UserRole), default=UserRole.operator)
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, server_default=func.now())


class Student(Base):
    __tablename__ = "students"

    id_ = Column(Integer, primary_key=True, index=True)
    student_id = Column(String(20), unique=True, index=True, nullable=False)
    name = Column(String(100), nullable=False)
    phone = Column(String(20))
    balance = Column(Float(), default=0.0)
    status = Column(String(20), default="active")
    created_at = Column(DateTime, server_default=func.now())

    # 关系
    sessions = relationship("ComputerSession", back_populates="student")
    recharges = relationship("StudentRecharge", back_populates="student")


class ComputerRoom(Base):
    __tablename__ = "computer_rooms"

    id_ = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), unique=True, nullable=False)
    location = Column(String(200))
    total_computers = Column(Integer, nullable=False)
    available_computers = Column(Integer, nullable=False)
    status = Column(Enum(RoomStatus), default=RoomStatus.active)
    hourly_rate = Column(Float(), default=5.0)
    created_at = Column(DateTime, server_default=func.now())

    # 关系
    machines = relationship("Machine", back_populates="room")


class Machine(Base):
    __tablename__ = "machines"

    id_ = Column(Integer, primary_key=True, index=True)
    room_id = Column(Integer, ForeignKey("computer_rooms.id_"), nullable=False)
    machine_number = Column(String(20), nullable=False)
    ip_address = Column(String(15))
    status = Column(Enum(MachineStatus), default=MachineStatus.available)
    created_at = Column(DateTime, server_default=func.now())

    # 关系
    room = relationship("ComputerRoom", back_populates="machines")
    sessions = relationship("ComputerSession", back_populates="machine")


class ComputerSession(Base):
    __tablename__ = "computer_sessions"

    id_ = Column(Integer, primary_key=True, index=True)
    student_id = Column(Integer, ForeignKey("students.id_"), nullable=False)
    machine_id = Column(Integer, ForeignKey("machines.id_"), nullable=False)
    start_time = Column(DateTime, nullable=False)
    end_time = Column(DateTime)
    duration_minutes = Column(Integer)
    hourly_rate = Column(Float(), nullable=False)
    total_cost = Column(Float())
    status = Column(Enum(SessionStatus), default=SessionStatus.active)
    created_at = Column(DateTime, server_default=func.now())

    # 关系
    student = relationship("Student", back_populates="sessions")
    machine = relationship("Machine", back_populates="sessions")


class StudentRecharge(Base):
    __tablename__ = "student_recharges"

    id_ = Column(Integer, primary_key=True, index=True)
    student_id = Column(Integer, ForeignKey("students.id_"), nullable=False)
    amount = Column(Float(), nullable=False)
    recharged_by = Column(Integer, ForeignKey("users.id_"), nullable=False)
    notes = Column(Text)
    created_at = Column(DateTime, server_default=func.now())

    # 关系
    student = relationship("Student", back_populates="recharges")
